<div class="row col-12">
  <div class="card">
    <div class="card-header">
      <h3 class="card-title">redis密码修改</h3>
    </div>
    <div class="card-body">
      <!-- BEGIN FORM-->
      <form class="form-horizontal form-bordered form-row-stripped">
        <div class="form-body">
          <#if customPassword?? && (customPassword != '')>
            <input type="hidden" id="oldPassword" name="oldPassword" value="${customPassword!}">
          </#if>
          <#if !(customPassword??) || (customPassword == '')>
            <input type="hidden" id="oldPassword" name="oldPassword" value="${pkey!}">
          </#if>
          <input type="hidden" id="appId" name="appId" value="${appId!}">
          <div class="form-group row">
            <label class="col-form-label col-md-3 text-end">
              redis密码
            </label>
            <div class="col-md-4">
              <#if customPassword?? && (customPassword != '')>
                <input type="text" name="password" id="password" value="${customPassword!}" class="form-control"/>
              </#if>
              <#if !(customPassword??) || (customPassword == '')>
                <input type="text" name="password" id="password" value="${pkey!}" class="form-control"/>
              </#if>
            </div>
            <div class="col-md-2">
              <#if customPassword?? && (customPassword != '')>
                <input type="checkbox" id="isSetPasswd" name="isSetPasswd" checked="checked"/>设置自定义密码
              </#if>
              <#if !(customPassword??) || (customPassword == '')>
                <input type="checkbox" id="isSetPasswd" name="isSetPasswd" />设置自定义密码
              </#if>
            </div>
            <div class="col-md-2">
              <button type="button" class="btn btn-sm btn-primary" onclick="updateAppPassword('${customPassword!}')">
                更新
              </button>
              <button type="button" class="btn btn-sm btn-primary" onclick="checkAppPassword()">
                校验
              </button>
            </div>
          </div>
          <div class="form-group row">
            <label class="col-form-label offset-md-3 col-md-6" style="color: orangered; text-align: left; ">
              自定义密码：用户定义的密码，设置的值即为密码；<br>
              默认密码：设置的值仅为基础值，对该值经过系统默认加密处理从而生成密码。<br>
              自定义密码优先级高于默认密码，如需清除密码，请先置空自定义密码，然后置空默认密码。
            </label>
          </div>
          <div class="form-group row">
            <label class="col-form-label offset-md-3 col-md-8" style="color: orange; text-align: left; ">
              请注意，redis6.0.0——6.0.8版本由于源码bug(#7899)，不支持清除密码 &nbsp;&nbsp;<a href="https://raw.githubusercontent.com/redis/redis/6.0/00-RELEASENOTES">点击查看</a>
            </label>
          </div>
        </div>
      </form>
      <!-- END FORM-->
    </div>
  </div>

  <#if aclEnable?? && aclEnable>
    <div class="card">
      <div class="card-header">
        <h3 class="card-title">acl用户密码</h3>
      </div>
      <div class="card-body">
        <table class="table table-bordered table-striped table-hover text-center">
          <thead>
            <tr>
              <th>用户名</th>
              <th>密码</th>
            </tr>
            </thead>
            <tbody>
            <tr>
              <td>cachecloud(管理员)</td><td>${adminPassword!}</td>
            </tr>
            <tr>
              <td>default(默认用户)</td><td>${defaultPassword!}</td>
            </tr>
            <tr>
              <td>readonly(只读用户)</td><td>${readonlyPassword!}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </#if>
</div>

<script>
    function updateAppPassword(customPwd) {
        var oldPassword = document.getElementById("oldPassword");
        var password = document.getElementById("password");
        var appId = document.getElementById("appId");
        var isSetPasswd = document.getElementById("isSetPasswd").checked;
        if(customPwd != null && customPwd != ''){
            if(isSetPasswd == true){
                if(oldPassword.value.trim() == password.value.trim()){
                    alert("密码未变更,不更新!");
                    return false;
                }
            }
        }
        if(customPwd == null || customPwd == ''){
            if(isSetPasswd == false){
                if(oldPassword.value.trim() == password.value.trim()){
                    alert("密码未变更,不更新!");
                    return false;
                }
            }
        }

        var originType = (customPwd != null && customPwd != '') ? "自定义密码" : "默认密码";
        var newType = isSetPasswd == true ? "自定义密码" : "默认密码";

        $.get(
            '${request.contextPath}/manage/app/updateAppPassword.json',
            {
                password: password.value.trim(),
                appId: appId.value,
                isSetPasswd: isSetPasswd
            },
            function(data){
                var status = data.status;
                if (status == 1) {
                    alert("设置成功! 原有:" + originType + "【"+oldPassword.value+"】已更新成新密码：" + newType + "【"+password.value+"】");
                    window.location.reload();
                    // $("#oldPassword").attr("value",password.value);
                } else {
                    alert("设置失败!");
                }
            }
        );
    }

    // 检验密码一致性
    function checkAppPassword() {
        //var password = document.getElementById("password");
        var appId = document.getElementById("appId");

        $.get(
            '${request.contextPath}/manage/app/checkAppPassword.json',
            {
                //password: password.value,
                appId: appId.value
            },
            function(data){
                var status = data.status;
                if (status == 1) {
                    alert("应用密码是有效且一致!");
                } else {
                    alert("应用密码是有效不一致!");
                }
            }
        );
    }
</script>


